package testDemo.Test2;
/*
* 在Person类中年龄的范围是0-120岁，性别只能是男或女
* 自定义年龄异常和性别异常类
* 编写测试类，给人的年龄和性别赋值，如果不符合年龄，抛出自定义异常类的异常提示信息
* 不在0-120岁"年龄不符合正常人类"
* 如果性别不是男或女"性别不匹配"
 * */

import java.util.Scanner;

class ageException extends Exception{
    public ageException(String s) {
        super(s);
    }
}
class sexException extends Exception{

    public sexException(String s) {
        super(s);
    }
}
class nameException extends Exception{

    public nameException(String s) {
        super(s);
    }
}
class Person {
    private String name;
    private int age;
    private String sex;

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws ageException {
        if(age<0 || age>120){
            throw new ageException("年龄不符合正常人类");
        }else{
            this.age = age;
        }
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) throws sexException {
        if(sex.equals("男") || sex.equals("女")){
            this.sex = sex;
        }else{
            throw new sexException("性别不匹配");
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) throws nameException {
        for (int i = 0; i < name.length(); i++) {
            if(name.substring(i,i+1).matches("[u4E00-u9FA5]+")){
                throw new nameException("姓名不符合,请输入中文");
            }
        }
        this.name = name;
    }
}
class ExceptionDemo{
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Person p = new Person();
        System.out.println("请输入您的姓名(中文)：");
        p.setName(sc.nextLine());
        System.out.println("请输入年龄:");
        p.setAge(sc.nextInt());
        System.out.println("请输入性别:");
        p.setSex(sc.next());
        System.out.println("创建成功");
        System.out.println("您的信息如下:");
        System.out.println("姓名:"+p.getName()+"\n"+"年龄:"+p.getAge()+"\n"+"性别:"+p.getSex());
    }
}
